package com.java.jigsaw.ui;

import cn.hutool.core.io.FileUtil;
import com.java.jigsaw.domain.User ;
import com.java.jigsaw.util.CodeUtil ;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

//登陆界面
public class LoginJFrame extends JFrame implements MouseListener {

    static Character sign = new JPasswordField().getEchoChar();
    ArrayList<User> allUsers = new ArrayList<>();

    JButton login = new JButton();
    JButton register = new JButton();

    JLabel visual = new JLabel();
    JLabel invisible = new JLabel();
    //存放显示的密码

    JTextField username = new JTextField();
    JPasswordField password = new JPasswordField();
    JTextField code = new JTextField();
    //正确的验证码
    JLabel rightCode = new JLabel();

    public LoginJFrame() {
        readUserInfo();
        //初始化界面
        initJFrame();
        //在这个界面中添加内容
        initView();
        //让当前界面显示出来
        this.setVisible(true);
    }

    private void readUserInfo() {
        List<String> userInfoStrList = FileUtil.readUtf8Lines("/Applications/IDEA/jigsaw/userinfo.txt");
        for (String str : userInfoStrList) {
            String[] userInfoArr = str.split("&");
            String[] arr1 = userInfoArr[0].split("=");
            String[] arr2 = userInfoArr[1].split("=");
            User u = new User(arr1[1],arr2[1]);
            allUsers.add(u);
        }
    }

    public void initView() {
        //添加用户名文字
        JLabel usernameText = new JLabel(new ImageIcon("image/login/用户名.png"));
        usernameText.setBounds(95, 145, 47, 17);
        this.getContentPane().add(usernameText);
        //添加用户名输入框
        username.setBounds(177, 135, 194, 40);
        //设置透明背景色
        username.setBackground(new Color(0, 0, 0, 0));
        this.getContentPane().add(username);
        JLabel usernamePicture = new JLabel(new ImageIcon("image/login/对话框.png"));
        usernamePicture.setBounds(175, 135, 198, 40);
        this.getContentPane().add(usernamePicture);

        //添加密码文字
        JLabel passwordText = new JLabel(new ImageIcon("image/login/密码.png"));
        passwordText.setBounds(110, 205, 32, 16);
        this.getContentPane().add(passwordText);
        //密码输入框
        visual = new JLabel(new ImageIcon("image/login/已移除背景的显示密码.png"));
        visual.setBounds(335, 207, 29, 18);
        visual.addMouseListener(this);
        this.getContentPane().add(visual);
        invisible = new JLabel(new ImageIcon("image/login/已移除背景的显示密码按下.png"));
        invisible.setBounds(335, 207, 29, 18);
        invisible.addMouseListener(this);
        this.getContentPane().add(invisible);
        invisible.setVisible(false);
        password.setBounds(177, 195, 194, 40);
        password.setBackground(new Color(0, 0, 0, 0));
        this.getContentPane().add(password);
        JLabel passwordPicture = new JLabel(new ImageIcon("image/login/对话框.png"));
        passwordPicture.setBounds(175, 195, 198, 40);
        this.getContentPane().add(passwordPicture);

//        //验证码文字
//        JLabel codeText = new JLabel(new ImageIcon("image/login/验证码.png"));
//        codeText.setBounds(115, 245, 50, 30);
//        this.getContentPane().add(codeText);
//        //验证码的输入框
//        code.setBounds(195, 245, 140, 30);
//        this.getContentPane().add(code);
//
//        //生成验证码
//        String codeStr = CodeUtil.getCode();
//        rightCode.setText(codeStr);
//        rightCode.addMouseListener(this);
//        rightCode.setBounds(340, 245, 50, 30);
//        this.getContentPane().add(rightCode);

        //添加登录按钮
        login.setBounds(90, 280, 128, 47);
        login.setIcon(new ImageIcon("image/login/登录按钮.png"));
        //去除按钮的边框
        login.setBorderPainted(false);
        //去除按钮的背景
        login.setContentAreaFilled(false);
        login.addMouseListener(this);
        this.getContentPane().add(login);

        //添加注册按钮
        register.setBounds(256, 280, 128, 47);
        register.setIcon(new ImageIcon("image/login/注册按钮.png"));
        register.setBorderPainted(false);
        register.setContentAreaFilled(false);
        register.addMouseListener(this);
        this.getContentPane().add(register);

        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("image/login/background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
    }

    public void initJFrame() {
        this.setSize(470, 417);
        this.setTitle("拼图游戏 V1.0登录");
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }

    //创建弹窗界面
    public static void showJDialog(String content) {
        JDialog jDialog = new JDialog();
        jDialog.setSize(200, 150);
        jDialog.setAlwaysOnTop(true);
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);
        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);
        //让弹框展示出来
        jDialog.setVisible(true);
    }

    //判断用户在集合中是否存在
    public boolean contains(User userInput){
        for (int i = 0; i < allUsers.size(); i++) {
            User rightUser = allUsers.get(i);
            if(userInput.getUsername().equals(rightUser.getUsername()) && userInput.getPassword().equals(rightUser.getPassword())){
                return true;
            }
        }
        return false;
    }

    //点击事件
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == login) {

            String usernameInput = username.getText();
            String passwordInput = new String(password.getPassword());
//            String codeInput = code.getText();
            //创建一个User对象
            User userInfo = new User(usernameInput, passwordInput);

            if (usernameInput.length() == 0 || passwordInput.length() == 0) {
                //调用showJDialog方法并展示弹框
                showJDialog("用户名或者密码为空");
            } else if (contains(userInfo)) {
                this.setVisible(false);
                //打开游戏的主界面
                //需要把当前登录的用户名传递给游戏界面
                new GameJFrame();
            } else {
                showJDialog("用户名或密码错误");
            }

//            if (codeInput.length() == 0) {
//                showJDialog("验证码不能为空");
//            } else if (usernameInput.length() == 0 || passwordInput.length() == 0) {
//                //调用showJDialog方法并展示弹框
//                showJDialog("用户名或者密码为空");
//            } else if (!codeInput.equalsIgnoreCase(rightCode.getText())) {
//                showJDialog("验证码输入错误");
//            } else if (contains(userInfo)) {
//                this.setVisible(false);
//                //打开游戏的主界面
//                //需要把当前登录的用户名传递给游戏界面
//                new GameJFrame();
//            } else {
//                showJDialog("用户名或密码错误");
//            }
        } else if (e.getSource() == register) {
            this.setVisible(false);
            new RegisterJFrame(allUsers);
        }else if(e.getSource() == visual){
            visual.setVisible(false);
            invisible.setVisible(true);
            password.setEchoChar((char) 0);
        }else if(e.getSource() == invisible){
            visual.setVisible(true);
            invisible.setVisible(false);
            password.setEchoChar(sign);
        }
//        else if (e.getSource() == rightCode) {
//            //获取一个新的验证码
//            String code = CodeUtil.getCode();
//            rightCode.setText(code);
//        }
    }
    //按下不松
    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == login) {
            login.setIcon(new ImageIcon("image/login/登录按下.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("image/login/注册按下.png"));
        }
    }
    //松开按钮
    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == login) {
            login.setIcon(new ImageIcon("image/login/登录按钮.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("image/login/注册按钮.png"));
        }
    }
    //鼠标划入
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    //鼠标划出
    @Override
    public void mouseExited(MouseEvent e) {

    }
}
